﻿using Common.Utils;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    [CustomActionFilte]
    public class WorkFlow_InstanceController : Controller
    {
        //工作流实例接口
        IWorkFlow_InstanceBLL _workFlow_InstanceBLL;
        IWorkFlow_ModelBLL _workFlow_ModelBLL;
        //用户接口
        IUserBLL _userBLL;

        //耗材信息接口
        IConsumableInfoBLL _consumableInfoBLL;
        public WorkFlow_InstanceController(IWorkFlow_InstanceBLL workFlow_InstanceBLL, IUserBLL userBLL, IConsumableInfoBLL consumableInfoBL, IWorkFlow_ModelBLL iL)
        {
            _workFlow_InstanceBLL = workFlow_InstanceBLL;
            _userBLL = userBLL;
            _consumableInfoBLL = consumableInfoBL;
            _workFlow_ModelBLL = iL;
        }

        #region 视图

        /// <summary>
        /// 工作流实例列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkFlow_InstanceView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataWorkFlow_InstanceView()
        {
            return View();
        }
        #endregion


        /// <summary>
        /// 工作流模板、工作流管理物品下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getWorkFlow_ModelSelect()
        {
            //部门数据集
            List<TransferVo> department = _workFlow_ModelBLL.getWorkFlow_ModelSelect();

            //获取主管数据集
            List<TransferVo> consumableInfo = _consumableInfoBLL.getOutGoodsSelect();
            var datas = new
            {
                department,
                consumableInfo
            };
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), datas));
        }

        /// <summary>
        /// 工作流实例列表、搜索
        /// </summary>
        /// <param name="workFlow_InstanceName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getWorkFlow_InstanceList(string workFlow_InstanceName, string page, string limit)
        {
            List<WorkFlow_InstanceDTO> workFlow_Instance = null;
            int cont = 0;
            try
            {
                workFlow_Instance = _workFlow_InstanceBLL.getworkFlow_InstanceList(workFlow_InstanceName, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(workFlow_Instance, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, workFlow_Instance));
        }

        /// <summary>
        /// 添加工作流实例
        /// </summary>
        /// <param name="workFlow_Instance">工作流实例信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddWorkFlow_Instance(WorkFlow_Instance workFlow_Instance)
        {
            //状态信息
            string mgs;
            object data = null;
            if (Verify(workFlow_Instance, out mgs))
            {
                try
                {
                    string userId = HttpContext.Session.GetString("userId");
                    if (string.IsNullOrEmpty(userId))
                    {
                        throw new MyException(900,"登录已过期");
                    }
                    workFlow_Instance.Creator = userId;
                    data = _workFlow_InstanceBLL.AddWorkFlow_Instance(workFlow_Instance);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? "发起成功" : mgs, data));
        }


        /// <summary>
        /// 作废工作流实例
        /// </summary>
        /// <param name="workFlow_InstanceId"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult DalateWorkFlow_Instance(string workFlow_InstanceId)
        {
            object data = null;
            try
            {
                data = _workFlow_InstanceBLL.DalateWorkFlow_Instance(workFlow_InstanceId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, "作废成功", data));
        }


        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="workFlow_Instance">参数</param>
        /// <returns></returns>
        private bool Verify(WorkFlow_Instance workFlow_Instance, out string mgs)
        {
            //if (String.IsNullOrEmpty(workFlow_Instance.WorkFlow_InstanceName))
            //{
            //    mgs = "工作流实例名称不能为空";
            //    return false;
            //}
            //else if (workFlow_Instance.LeaderId.EndsWith("0"))
            //{
            //    mgs = "主管不能为空";
            //    return false;
            //}
            //else if (String.IsNullOrEmpty(workFlow_Instance.ParentId))
            //{
            //    mgs = "父工作流实例不能为空";
            //    return false;
            //}
            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
